package com.example.shardingsphere;

import com.example.shardingsphere.sharding.UserIdHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * describe: 测试用随便写写
 *
 * @author lianying
 * @date 2023/05/05
 */
@RestController
public class TestController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private UserIdHandler userIdHandler;

    @GetMapping("/user/{id}")
    public List get(@PathVariable Long id){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from t_user where id=" + id);
        return maps;
    }

    @PostMapping("/user")
    public String add(Integer age,String name){
        //需要生成ID
        User user=new User();
        user.setAge(age);
        user.setName(name);
        Long id = userIdHandler.createId(user);
        String sql="insert into t_user(id,age,name) values(%s,%s,%s);";
        jdbcTemplate.execute(String.format(sql,id,age,name));
        return "新增成功";
    }
}
